package com.insurance.service.impl;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.mapper.AuthorityMapper;
import com.insurance.pojo.Information;
import com.insurance.pojo.UserPermissions;
import com.insurance.service.AuthorityService;
import com.insurance.utils.ConversionDateUtils;
import com.insurance.utils.PasswordEncryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/29 21:13
 * Description:
 * Version: V1.0
 */

@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    private AuthorityMapper authorityMapper;

    @Override
    public ResultData addInform(Information information) throws SQLException {
        ResultData resultData=new ResultData();
        resultData.setCode(Contants.FAILURE);
        resultData.setMsg("推送失败");
        information.setInformationTime(ConversionDateUtils.conversionDateStringUtils(new Date()));
        int j=0;
            if (information.getReceiver().length==1 && information.getReceiver()[0].contains("司机")){
                //1为司机
                j=1;
            }else if (information.getReceiver().length==1&&information.getReceiver()[0].contains("用户")){
                //2为用户
                j=2;
            }
        int infoId=authorityMapper.addInform(information);
        //给司机加
        int info_id=information.getInfo_id();
        if (j==1){
            int[] driverId=authorityMapper.queryDriver();
            int res=authorityMapper.addInformDriver(info_id,driverId);
            if (res>0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("推送成功");
                return resultData;
            }
                return resultData;
            //给用户加
        }else if (j==2){
            int[] userId=authorityMapper.queryUserId();
            int res=authorityMapper.addInformUser(info_id,userId);
            if (res>0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("推送成功");
                return resultData;
            }
            return resultData;
        }else {//都加
            int[] userId=authorityMapper.queryUserId();
            int res2=authorityMapper.addInformUser(info_id,userId);
            int[] driverId=authorityMapper.queryDriver();
            int res=authorityMapper.addInformDriver(info_id,driverId);
            if (res>0){
                resultData.setCode(Contants.SUCCESS);
                resultData.setMsg("推送成功");
                return resultData;
            }
            return resultData;
        }
    }

    @Override
    public ResultData queryInform() {
        ResultData resultData=new ResultData();
        List<Map> list=authorityMapper.queryInform();
        if (list.size()>0){
            for (Map map:list) {
                map.put("information_time",ConversionDateUtils.conversionDateStringUtils(map.get("information_time")));
            }
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData delInform(Information information) {
        ResultData resultData=new ResultData();
        int res=authorityMapper.delInform(information);
        if (res>0){
            resultData.setMsg("删除成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setMsg("系统开小差...");
            resultData.setCode(Contants.FAILURE);
        }
        return resultData;
    }

    @Override
    public ResultData queryAuthorityList() {
        ResultData resultData=new ResultData();
        List<Map> list=authorityMapper.queryAuthorityList();
        for (Map map1:list){
            int[] strings= authorityMapper.querypermissions(map1.get("userId"));
            map1.put("user_permissions",strings);
        }
        if (list.size()>0){
            for (Map map:list) {
                if (map.get("userLastTime") !=null){
                map.put("userLastTime",ConversionDateUtils.conversionDateStringUtils(map.get("userLastTime")));
                }else {
                    map.put("userLastTime","从未登录");
                }
            }
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData addAuthority(UserPermissions userPermission) {
        ResultData resultData=new ResultData();
        String pwd=userPermission.getUserPwd();
        userPermission.setUserPwd(PasswordEncryption.passwordEncryption(pwd));
        authorityMapper.addAuthority(userPermission);
        int user_id=userPermission.getUser_id();
        int result=authorityMapper.addUserPermission(user_id,userPermission.getPermissions());
        if (result>0){
            resultData.setMsg("添加成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setMsg("添加失败");
            resultData.setCode(Contants.SUCCESS);
        }
        return resultData;
    }

    @Override
    public ResultData editAuthority(UserPermissions userPermission) {
        ResultData resultData=new ResultData();
        String pwd=userPermission.getUserPwd();
        userPermission.setUserPwd(PasswordEncryption.passwordEncryption(pwd));
        authorityMapper.updateAuthority(userPermission);
        int user_id=userPermission.getUser_id();
        int delPermissionUser=authorityMapper.delPermissionUser(user_id);
        int result=authorityMapper.addUserPermission(user_id,userPermission.getPermissions());
        if (result>0){
            resultData.setMsg("修改成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setMsg("修改失败");
            resultData.setCode(Contants.SUCCESS);
        }
        return resultData;
    }

    @Override
    public ResultData editAuthorityStatu(Integer userId, Integer userStatu) {
        ResultData resultData=new ResultData();
        int update=authorityMapper.editAuthorityStatu(userId,userStatu);
        if (update>0){
            resultData.setMsg("修改成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setMsg("修改失败");
            resultData.setCode(Contants.SUCCESS);
        }
        return resultData;
    }

    @Override
    public ResultData deleteAuthority(Integer userId) {
        ResultData resultData=new ResultData();
        int deleteAuthority=authorityMapper.deleteAuthority(userId);
        if (deleteAuthority>0){
            resultData.setMsg("修改成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setMsg("修改失败");
            resultData.setCode(Contants.SUCCESS);
        }
        return resultData;
    }
    @Override
    public ResultData queryCityAll() {
        ResultData resultData=new ResultData();
        //查省
        List<Map> list=authorityMapper.queryPerAll();
        for (Map map:list){
               List<Map> list1= authorityMapper.queryCityAll(map.get("province_id"));
               map.put("city_name",list1);
            for (Map map1:list1){
                if (map1.get("opening_time")!=null){
                map1.put("opening_time",ConversionDateUtils.conversionDateStringUtils(map1.get("opening_time")));
                }
                   List<Map> list2= authorityMapper.queryAreaAll(map1.get("city_id"));
                       String[] strings=new String[list2.size()];
                       int j=0;
                   for (Map map2:list2) {
                       strings[j]=map2.get("area_name").toString();
                       j++;
                   }
                   map1.put("area_name",strings);
               }
        }
        if (list.size()>0){
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryCityAllCount() {
        ResultData resultData=new ResultData();
        //查省
        List<Map> list=authorityMapper.queryCityAllCount();
        if (list.size()>0){
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryPerAll() {
        ResultData resultData=new ResultData();
        //查省
        List<Map> list=authorityMapper.queryPerAll();
        if (list.size()>0){
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryByCityStrtu(int province_id) {
        ResultData resultData=new ResultData();
        //查未开通市
        List<Map> list=authorityMapper.queryByCityStrtu(province_id);
        if (list.size()>0){
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData addCity(int cityId) {
        ResultData resultData=new ResultData();
        int result=authorityMapper.addCity(cityId);
        if (result>0){
            resultData.setMsg("添加成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("添加失败");
        }
        return resultData;
    }

    @Override
    public ResultData deleteCity(int cityId) {
        ResultData resultData=new ResultData();
        int result=authorityMapper.deleteCity(cityId);
        if (result>0){
            resultData.setMsg("删除成功");
            resultData.setCode(Contants.SUCCESS);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("删除失败");
        }
        return resultData;
    }

}
